

product_list = [["iphone",6688],["mac",8999],["小米手机",2000],["锤子手机",1500]]
shopping_car = []
shopping_total = 0
salary = int(input("请输入你的工资："))

while True:
    for i  in product_list:
        print(product_list.index(i),i[0],i[1])

    choice = input("请输入你要购买的商品代号（退出请输入q）:")
    if choice.isdigit():
        choice = int(choice)
        if choice>0 and choice< len(product_list):
            shopping_car.append(product_list[choice])
            shopping_total = shopping_total +product_list[choice][1]
            if salary - shopping_total < 0:
                print("余额不足")
            print("购物车已经添加如下商品：%s" % shopping_car)
        else:
            print("输入超出范围，请重新输入！")

    elif choice == "Q" or choice == "q":
        break
    else:
        print("输入错误，请重新输入")
print("你总共购买以下商品%s, 消费%s元, 剩余%s元。"% (shopping_car,shopping_total ,(salary-shopping_total)))

